<template>
  <d2-container>
    <!-- <el-form :inline="true" :model="dataSearch" ref="dataSearch">
            <el-form-item label="" prop="">
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="formSubmit" size="mini">
                    <d2-icon name="search"/>
                    查询
                </el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="formReset" size="mini">
                    <d2-icon name="refresh"/>
                    重置
                </el-button>
            </el-form-item>
            <a href="#/student-new">
                <el-button type="primary" style="float:right" icon="el-icon-plus" size="mini">新建学生</el-button>
            </a>
    </el-form>-->
    <el-table :data="dataList" border style="width: 100%">
      <el-table-column prop="class.name" label="班级名称"></el-table-column>
      <el-table-column prop="dateText" label="日期"></el-table-column>
      <el-table-column prop="teacher.name" label="教师姓名"></el-table-column>
      <el-table-column prop="tempTeacher.name" label="代课教师姓名"></el-table-column>
      <el-table-column prop="school.name" label="校区"></el-table-column>
      <el-table-column prop="statusText" label="状态"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="agree(scope.row)" type="text" size="small">同意</el-button>
          <el-button @click="refuse(scope.row)" type="text" size="small">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes,prev, pager, next,jumper"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handlerSizeChange"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import util from "@/libs/util.js";
import formatTime from "@/libs/util.formatTime";
import {mapState} from "vuex";

export default {
  data() {
    return {
      dataList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      dataSearch: {}
    };
  },
  computed: {
    ...mapState('d2admin/user', ['info']),
  },
  methods: {
    formSubmit() {
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    formReset() {
      this.$refs.dataSearch.resetFields();
    },
    handleClick(e) {
      this.$router.push({ path: "/student-detail?id=" + e._id });
    },

    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlerSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },

    getDataList(data) {
      data.uuid = util.cookies.get("uuid");
      data.page = this.currentPage;
      data.pageSize = this.pageSize;
      this.dataSearch.limitSchool = this.info.school.map(value => value._id);
      data.limitSchool = this.dataSearch.limitSchool;
      request({
        url: "/substitute/getSubstituteList",
        method: "post",
        data: data
      }).then(res => {
        this.dataList = [];
        res.data.submitList.forEach((value, index) => {
          value.statusText =
            value.status == 1
              ? "审核中"
              : value.status == 2
              ? "通过"
              : value.status == 3
              ? "拒绝"
              : "";
          (value.dateText = this.formatDate(value.date)), "dada";
        });
        this.dataList = res.data.submitList;
        this.total = res.data.total;
        this.currentPage = res.data.currentPage;
      });
    },
    formatDate(date) {
      var date = new Date(date);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      return [year, month, day].map(this.formatNumber).join("-");
    },
    formatNumber(n) {
      n = n.toString();
      return n[1] ? n : "0" + n;
    },
    agree(e) {
      this.$confirm("通过申请, 是否确认?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$alert("请确认已通知代课教师", "操作确认", {
            confirmButtonText: "确定",
            callback: action => {
              if (action == 'confirm') {
                request({
                  url: "/substitute/updateSubstitute",
                  method: "post",
                  data: { status: 2, id: e._id }
                }).then(res => {
                  if (res.code == 200) {
                    this.getDataList(this.dataSearch);
                  }
                });
              }
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消审核"
          });
        });
    },
    refuse(e) {
      this.$confirm("拒绝申请, 是否确认?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/substitute/updateSubstitute",
            method: "post",
            data: { status: 3, id: e._id }
          }).then(res => {
            if (res.code == 200) {
              this.getDataList(this.dataSearch);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消审核"
          });
        });
    }
  },
  created() {
    this.dataSearch.page = this.currentPage;
    this.dataSearch.pageSize = this.pageSize;
    this.getDataList(this.dataSearch);
  }
};
</script>
